# Updated on 2013/11/07 night by Wei at school
# Updated on 2014/02/05 night by Wei at school

from __future__ import division
from operator import itemgetter, attrgetter

import gc
import math
import matplotlib
import os
import pylab
import random
import sys
import time
from sets import Set
from scipy import stats
import numpy as np

print "Program Begins..."
# experiments run 2 months ago
# weight 0
# inputFileName1 = "/home/diaosi/web-search-engine-wei/polyIRIndexer/selectedDocumentPrefixValuesInfo20131107Morning"
# weight 1
# inputFileName1 = "/home/diaosi/web-search-engine-wei/polyIRIndexer/selectedDocumentPrefixValuesInfo20131107Afternoon_weight_1000"
# weight 1000
# inputFileName1 = "/home/diaosi/web-search-engine-wei/polyIRIndexer/selectedDocumentPrefixValuesInfo20131107Afternoon_weight_1000"

# experiments run since 2014/02/05 night
inputFileName1 = "/home/diaosi/web-search-engine-wei/polyIRIndexer/selectedDocumentPrefixValuesInfo20140205Afternoon_weight_1000"
inputFileHandler1 = open(inputFileName1,"r")
inputFileName2 = "/home/diaosi/web-search-engine-wei/polyIRIndexer/selectedDocumentPostingValuesInfo20140205Night_weight_1000"
inputFileHandler2 = open(inputFileName2,"r")
lineCounter = 0

currentLineFromFile1 = inputFileHandler1.readline()
currentLineFromFile2 = inputFileHandler2.readline()
lineCounter += 1

while currentLineFromFile1:
    currentLineElements = currentLineFromFile1.strip().split(" ")
    currentDocID = currentLineElements[0]
    numOfPostingsRecorded = int( currentLineElements[1] )
    
    print "currentDocID: ",currentDocID
    print "numOfPostingsRecorded: ",numOfPostingsRecorded
    # This is just a CHECK:
    if numOfPostingsRecorded != len(currentLineElements[2:-2]) / 2:
        print "lineCounter:",lineCounter
        print "numOfPostingsRecorded:",numOfPostingsRecorded
        print "len(currentLineElements[2:]):",len(currentLineElements[2:])
        exit(1)
    else:
        pass
    
    # step0: init
    denominator = float( currentLineElements[-3] )
    xPoints = []
    yPoints = []
    
    # Let's plot the graph for 1 document first
    # step1: LOAD
    baseIndex = 2
    for i in range(0,numOfPostingsRecorded):
        termID = currentLineElements[ baseIndex ]
        numerator = float( currentLineElements[ baseIndex + 1 ] )
        print i + 1,numerator,denominator
        xPoints.append(i + 1)
        yPoints.append( numerator / denominator)    
        baseIndex += 2
    #for index,currentValue in enumerate(currentLineElements[2:]):
    #    numerator = float(currentValue)
    #    print index + 1,numerator,denominator
    #    xPoints.append(index + 1)
    #    yPoints.append( numerator / denominator)
    
    # step2: DISPLAY
    matplotlib.pyplot.scatter(xPoints,yPoints)
    matplotlib.pyplot.show()
    
    
    #########################################################################
    currentLineElements = currentLineFromFile2.strip().split(" ")
    currentDocID = currentLineElements[0]
    numOfPostingsRecorded = int( currentLineElements[1] )
    
    print "currentDocID: ",currentDocID
    print "numOfPostingsRecorded: ",numOfPostingsRecorded
    # This is just a CHECK:
    if numOfPostingsRecorded != len(currentLineElements[2:-2]) / 2:
        print "lineCounter:",lineCounter
        print "numOfPostingsRecorded:",numOfPostingsRecorded
        print "len(currentLineElements[2:]):",len(currentLineElements[2:])
        exit(1)
    else:
        pass
    
    # step0: init
    denominator = 0
    xPoints = []
    yPoints = []

    # compute the denominator
    baseIndex = 2
    for i in range(0,numOfPostingsRecorded):
        termID = currentLineElements[ baseIndex ]
        numerator = float( currentLineElements[ baseIndex + 1 ] )
        denominator += numerator 
        baseIndex += 2
    print "denominator: ",denominator
    
    
    # Let's plot the graph for 1 document first
    # step1: LOAD
    baseIndex = 2
    for i in range(0,numOfPostingsRecorded):
        termID = currentLineElements[ baseIndex ]
        numerator = float( currentLineElements[ baseIndex + 1 ] )
        print i + 1,numerator,denominator
        xPoints.append(i + 1)
        yPoints.append( numerator / denominator)    
        baseIndex += 2
    #for index,currentValue in enumerate(currentLineElements[2:]):
    #    numerator = float(currentValue)
    #    print index + 1,numerator,denominator
    #    xPoints.append(index + 1)
    #    yPoints.append( numerator / denominator)
    
    # step2: DISPLAY
    matplotlib.pyplot.scatter(xPoints,yPoints)
    matplotlib.pyplot.show()
    
    
    
    currentLineFromFile1 = inputFileHandler1.readline()
    currentLineFromFile2 = inputFileHandler2.readline()
    
    lineCounter += 1
    # JUST for today 2013/11/07 afternoon by Wei at school
    if lineCounter >= 10:
        break

print "lineCounter:",lineCounter
print "The test has passed."
inputFileHandler1.close()
inputFileHandler2.close()
print "Program Ends."
